home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / script / HTML2XFC.php
Encoding:
PHP Script  |  2007-12-20  |  5.7 KB  |  220 lines

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  5.  
  6. /**
  7.  * Convertion tool to convert a HTML page to the XML_FastCreate syntax.
  8.  *
  9.  * Syntaxe : 
  10.  *     HTML2XFC <source_file.html> [<destination_file.php>]
  11.  * 
  12.  * Or by selecting text with your prefered editor. 
  13.  * With VIM, add this line into your ~/.vimrc :
  14.  *      map ,fc :!HTML2XFC.php<CR>
  15.  * Select your HTML text and call the script with ,fc
  16.  *
  17.  * PHP versions 4 and 5
  18.  *
  19.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  20.  * that is available through the world-wide-web at the following URI:
  21.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  22.  * the PHP License and are unable to obtain it through the web, please
  23.  * send a note to license@php.net so we can mail you a copy immediately.
  24.  *
  25.  * @category   XML
  26.  * @package    XML_FastCreate
  27.  * @author     Guillaume Lecanu <Guillaume@dev.fr>
  28.  * @copyright  1997-2005 The PHP Group
  29.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  30.  * @version    CVS: $Id: HTML2XFC.php,v 1.1 2005/12/08 14:42:09 guillaume Exp $
  31.  * @link       http://pear.php.net/package/XML_FastCreate
  32.  */
  33.  
  34. require_once('XML/XML_HTMLSax.php');
  35. error_reporting(E_ALL);
  36.  
  37. $header = "
  38. require_once 'XML/FastCreate/Text.php';
  39.  
  40. ".'$x'." =& XML_FastCreate_Text(
  41.     array(
  42.         'doctype'   => XML_FASTCREATE_DOCTYPE_XHTML_1_0_STRICT,
  43.         'quote'     => false
  44.     )
  45. );
  46. ";
  47.  
  48. $footer = '
  49. $x->toXML();
  50. ';
  51.  
  52.  
  53. // Define a customer handler class
  54. class XML_FastCreate_MyHandler {
  55.  
  56.  var $output = '';
  57.  var $tab = 0;
  58.  var $singleTag = array(
  59.                         'area' => 1,
  60.                         'basefont' => 1,
  61.                         'base' => 1,
  62.                         'br' => 1,
  63.                         'hr' => 1,
  64.                         'img' => 1,
  65.                         'input' => 1,
  66.                         'link' => 1,
  67.                         'meta' => 1,
  68.                     );
  69.     
  70.     // Make indentation
  71.     function addTab() 
  72.     {
  73.         $html = '';
  74.         for ($i=0; $i < $this->tab; $i++) {
  75.             $html .= "\t";
  76.         }
  77.         return "\n".$html;
  78.     }
  79.  
  80.  
  81.     // Opening tags
  82.     function openHandler(& $parser, $tagname, $attr) 
  83.     {
  84.         $tag = $tagname;
  85.         $html = '$x->'.$tag.'(';
  86.         $html_attr = '';
  87.         foreach ($attr as $key => $val) {
  88.             $html_attr .= "'$key'=>\"$val\", ";
  89.         }
  90.         if ($html_attr) {
  91.             $html_attr = substr($html_attr, 0, -2);
  92.             $html .= "array($html_attr)";
  93.         }
  94.         if (isSet($this->singleTag[$tagname])) {
  95.             $html .= "),";
  96.         } else {
  97.             if ($html_attr) {
  98.                 $html .= ',';
  99.             }
  100.         }
  101.         $this->output .= $this->addTab().$html;
  102.  
  103.         if (!isSet($this->singleTag[$tagname])) {
  104.             $this->tab++;
  105.         }
  106.     }
  107.  
  108.     // Closing tags
  109.     function closeHandler(& $parser, $tagname) 
  110.     {
  111.         $html = $join = '';
  112.         if (!isSet($this->singleTag[$tagname])) {
  113.             if (preg_match("/x->$tagname\($/", $this->output)) {
  114.                 $html .= "), ";
  115.                 $this->tab--;
  116.             } else {
  117.                 $this->output = substr($this->output, 0, 
  118.                     strlen($this->output)-1);
  119.                 $this->tab--;
  120.                 $html .= $this->addTab()."), ";
  121.             }
  122.             if (preg_match("/[\.,]\s*$/", $this->output)) {
  123.                 $this->output = preg_replace("/[\.,](\s)*$/", "$1", 
  124.                                     $this->output);
  125.             }
  126.             $this->output .= $html;
  127.         }
  128.     }
  129.  
  130.     // Text node handler
  131.     function dataHandler(& $parser, $text) 
  132.     {
  133.         $text = preg_replace("/^\s+/", "", $text);
  134.         $text = preg_replace("/\s+$/", "", $text);
  135.         $text = preg_replace("/\r/", "", $text);
  136.         if ($text) {
  137.             $this->output .= $this->addTab().'"'.$text.'". ';
  138.         }
  139.     }
  140.  
  141.     // XML escape handler (e.g. HTML comments)
  142.     function escapeHandler(& $parser,$data) 
  143.     {
  144.         $this->output .= $this->addTab().'$x->comment("'
  145.             .str_replace('"', '\"', $data).'"),';
  146.     }
  147.  
  148.     // Processing instruction handler
  149.     function piHandler(& $parser,$target,$data) 
  150.     {
  151.         $this->output .= '<?php'.$data.'?>';
  152.     }
  153.  
  154.     // Return output
  155.     function getOutput()
  156.     {
  157.         return $this->output;
  158.     }
  159. }
  160.  
  161. $doc = '';
  162. // Get the content from a file 
  163. if (isSet($_SERVER['argv'][1])) {
  164.     $doc = file_get_contents($_SERVER['argv'][1]);
  165.     if ($doc == FALSE) {
  166.         die("File not found !");
  167.     }
  168.     
  169. // Or get the content by STDIN
  170. } else {
  171.     $stdin = fopen('php://stdin', 'r');
  172.     while (!feof($stdin)) {
  173.         $doc .= fgets($stdin);
  174.     }
  175. }
  176.  
  177.  
  178. // Instantiate the handler
  179. $handler=new XML_FastCreate_MyHandler;
  180.  
  181. // Instantiate the parser
  182. $parser=& new XML_HTMLSax;
  183.  
  184. // Register the handler with the parser
  185. $parser->set_object($handler);
  186.  
  187. // Set the callback handlers (XML_FastCreate_MyHandler methods)
  188. $parser->set_element_handler('openHandler','closeHandler');
  189. $parser->set_data_handler('dataHandler');
  190. $parser->set_escape_handler('escapeHandler');
  191. $parser->set_pi_handler('piHandler');
  192.  
  193. // Parse the document
  194. $parser->parse($doc);
  195. $output = $handler->getOutput();
  196. if (isSet($_SERVER['argv'][1])) {
  197.     $output = substr($output, 0, -2).";\n\n";
  198.     $output = "<?php\n".$header.$output.$footer."\n?>";
  199. }
  200.  
  201. // Send output to a new file 
  202. if (isSet($_SERVER['argv'][2])) {
  203.     $filename = $_SERVER['argv'][2];
  204.     if (!$handle = fopen($filename, 'w+')) {
  205.         echo "Cannot open file ($filename)\n";
  206.         exit;
  207.     }
  208.     if (fwrite($handle, $output) === FALSE) {
  209.         echo "Cannot write to file ($filename)\n";
  210.         exit;
  211.     }
  212.     fclose($handle);
  213.  
  214. // Or send output to STDOUT
  215. } else {
  216.     echo $output;
  217. }
  218.  
  219. ?>
  220.